#region Using directives

using System;

#endregion

namespace MobileConfig
{
	/// <summary>
	/// Summary description for Utility.
	/// </summary>
	public class Utility
	{

		public static RegistryImpl.RootKey GetRootKey(string Key)
		{
			RegistryImpl.RootKey rk = RegistryImpl.RootKey.HKEY_LOCAL_MACHINE;

			switch (Key.ToLower())
			{
				case "hklm":
					rk = RegistryImpl.RootKey.HKEY_LOCAL_MACHINE;
					break;
				case "hkcu":
					rk = RegistryImpl.RootKey.HKEY_CURRENT_USER;
					break;
			}

			return rk;
		}

		public static void SetRegKeyValue(RegistryImpl.RootKey RootKey, string SubKey, string Value, object Data)
		{
			RegistryKey rk = Registry.UnknownKey(RootKey);
			string[] Subs = SubKey.Split('\\');
			foreach (string Sub in Subs)
			{
				bool found = false;
				foreach (string keyName in rk.GetSubKeyNames())
					if (keyName.ToLower() == Sub.ToLower())
						found = true;

				if (!found)
					rk = rk.CreateSubKey(Sub);
				else
					rk = rk.OpenSubKey(Sub);
			}
			rk.SetValue(Value, Data);
		}

		public static object GetRegKeyValue(RegistryImpl.RootKey RootKey, string SubKey, string Value, object DefaultValue)
		{
			RegistryKey rk = Registry.UnknownKey(RootKey);
			string[] Subs = SubKey.Split('\\');
			foreach (string Sub in Subs)
			{
				bool found = false;
				foreach (string keyName in rk.GetSubKeyNames())
					if (keyName.ToLower() == Sub.ToLower())
						found = true;

				if (!found)
					return null;
				else
					rk = rk.OpenSubKey(Sub);
			}
			return rk.GetValue(Value, DefaultValue);
		}
	}
}
